import re

from django.contrib.auth.backends import ModelBackend

from users.models import User


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据

    :param token:
    :param user:
    :param request:
    :return:
    """
    return {
        'token':token,
        'user_id':user.id,
        'username':user.username
    }


def get_user_by_account(account):
    """
    根据账号获取用户对象
    :param account:
    :return:
    """
    try:
        # 如果匹配到手机号
        if re.match(r'^1[3-9]\d{9}$',account):
            user = User.objects.get(mobile=account)
        else:
            user = User.objects.get(username=account)
    except User.DoesNotExist:
        return None
    else:
        return user


class UsernameMobileAuthBackend(ModelBackend):
    """
    自定义用户名或者手机号验证
    """
    def authenticate(self, request, username=None, password=None, **kwargs):
        """
        :param request:
        :param username:  # 可能是用户名也可能是手机号
        :param password:
        :param kwargs:
        :return:
        """
        # 获取对象
        user = get_user_by_account(username)
        if user is not None and user.check_password(password):
            return user






